/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.setting.item.backexit;

import android.content.Context;
import android.os.RemoteException;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.sidebar.SettingData;
import com.coocaa.x.service.XServiceManager;
import com.tianci.appstore.utils.SkyConstants;

public class BackExit extends Item {
    private boolean mBackExit = false;
    private SettingData mSettingData = null;
    public static final String BACK_EXIT = "back_exit";

    public BackExit(Context context) {
        super(context);
        try {
            mBackExit = XServiceManager.getLiteConfigService().getBooleanValue(BACK_EXIT, true);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ((BackExitItem) mItemView).setToggleButtonCheck(mBackExit);
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        // TODO Auto-generated method stub
        super.onItemFocusChange(v, focus);
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(View v) {
        try {
            XServiceManager.getLiteConfigService().saveBooleanConfig(BACK_EXIT, !mBackExit);
            ((BackExitItem) mItemView).setToggleButtonCheck(!mBackExit);
            mBackExit = !mBackExit;
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onItemClick(v);
    }

    @Override
    public ItemView createItemView() {
        // TODO Auto-generated method stub
        return new BackExitItem(mContext);
    }

    @Override
    public void setData(Object object) {
        this.mSettingData = (SettingData) object;
        ((BackExitItem) mItemView).updateIcon(SkyConstants.getIconId(mSettingData.iconId));
        ((BackExitItem) mItemView).updateName(SkyConstants.getStringId(mSettingData.titleId));
    }
}
